package org.feng.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.feng.annotation.AccessLog;
import org.feng.condition.Condition;
import org.feng.base.entity.Result;
import org.feng.entity.SysScheduler;
import org.feng.service.ISysSchedulerService;
import org.feng.support.SchedulerSupport;
import org.springframework.web.bind.annotation.*;
import org.feng.base.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import jakarta.annotation.Resource;

import java.util.List;
import java.util.Arrays;

/**
 * 定时任务表
 *
 * @author : FengShuai
 * @since : 2024/6/27 9:24
 */
@Tag(name = "定时任务表")
@RestController
@RequestMapping("/system/sysScheduler")
public class SysSchedulerController extends BaseController {

    @Resource
    private ISysSchedulerService sysSchedulerService;
    @Resource
    private SchedulerSupport schedulerSupport;

    /**
     * 分页列表
     *
     * @param sysScheduler 查询体
     * @return 返回体
     */
    @GetMapping("/pageList")
    @Operation(summary = "分页列表")
    public Result<IPage<SysScheduler>> pageList(SysScheduler sysScheduler) {
        return Result.success(sysSchedulerService.page(Condition.getPage(), Wrappers.lambdaQuery(sysScheduler)
                .like(StrUtil.isNotEmpty(sysScheduler.getName()), SysScheduler::getName, sysScheduler.getName())
                .eq(sysScheduler.getStatus() != null, SysScheduler::getStatus, sysScheduler.getStatus())));
    }

    /**
     * 列表
     *
     * @param sysScheduler 查询体
     * @return 返回体
     */
    @GetMapping("/list")
    @Operation(summary = "列表")
    public Result<List<SysScheduler>> list(SysScheduler sysScheduler) {
        return Result.success(sysSchedulerService.list(Wrappers.lambdaQuery(sysScheduler)));
    }

    /**
     * 保存
     *
     * @param sysScheduler 保存体
     * @return 返回体
     */
    @PostMapping("/save")
    @AccessLog("保存定时任务表")
    @Operation(summary = "保存")
    public Result<Boolean> save(@RequestBody SysScheduler sysScheduler) {
        return Result.status(sysSchedulerService.saveOrUpdateAndRun(sysScheduler));
    }

    /**
     * 更新
     *
     * @param sysScheduler 更新体
     * @return 返回体
     */
    @PostMapping("/update")
    @AccessLog("更新定时任务表")
    @Operation(summary = "更新")
    public Result<Boolean> update(@RequestBody SysScheduler sysScheduler) {
        return Result.status(sysSchedulerService.saveOrUpdateAndRun(sysScheduler));
    }

    /**
     * 删除
     *
     * @param ids 逗号分割的id
     * @return 返回体
     */
    @GetMapping("/remove")
    @AccessLog("删除定时任务表")
    @Operation(summary = "删除")
    public Result<Boolean> remove(@RequestParam("ids") String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        for (String id : idList) {
            schedulerSupport.delete(id);
        }
        return Result.status(sysSchedulerService.removeBatchByIds(idList));
    }
}
